import 'package:distribution_mall_flutter/assets.dart';
import 'package:distribution_mall_flutter/constant/cf_constant.dart';
import 'package:distribution_mall_flutter/constant/image_constant.dart';
import 'package:distribution_mall_flutter/constant/point_constant.dart';
import 'package:distribution_mall_flutter/entity/goods/detail/goods_detail_entity.dart';
import 'package:distribution_mall_flutter/generated/l10n.dart';
import 'package:distribution_mall_flutter/pages/goods/product_details/application/product_details_service.dart';
import 'package:distribution_mall_flutter/pages/goods/product_details/product_details_provider.dart';
import 'package:distribution_mall_flutter/provider/goods/product_details.dart';
import 'package:distribution_mall_flutter/provider/user/login.dart';
import 'package:distribution_mall_flutter/utils/firebase/firebase_analytics_utils.dart';
import 'package:distribution_mall_flutter/utils/image_loader.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class ProductAddress extends ConsumerStatefulWidget {
  final String saleItemId;

  const ProductAddress(this.saleItemId, {super.key});

  @override
  ConsumerState createState() => _ProductAddress();
}

class _ProductAddress extends ConsumerState<ProductAddress> {

  @override
  Widget build(BuildContext context) {
    ///地址
    var userAddress = ref.watch(userAddressProvider);

    ///用户选择的地址
    var addressInfo = userAddress.userSelectAddress;

    ///地址列表
    var addressList = userAddress.addressList;

    ///配送时间
    var addrDeTime = userAddress.addrDeTime;
    List<Sale> list =
        ref.watch(pDetailProvider.select((value) => value.saleInfos));

    bool isLogin = ref.watch(loginStateProvider);
    ref.listen(loginStateProvider, (pre, next) {
      if (pre != next && next) {
        ref
            .read(productDetailsServiceProvider(widget.saleItemId))
            .initAddress();
      }
    });
    return SliverToBoxAdapter(
      child: GestureDetector(
        onTap: () async {
          FirebaseAnalyticsUtils.logEvent(
            name: PointConstant.pdAddress,
          );
          ref
              .read(productDetailsServiceProvider(widget.saleItemId))
              .selectAddress(context, addressList);
        },
        child: Container(
          padding: const EdgeInsets.only(left: 16,right: 16,top: 36,bottom: 16),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 4),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          Padding(
                            padding: const EdgeInsets.only(right: 4),
                            child: ImageLoader.imageAsset(ImageLocalConstant.location,
                                commonWH: 21.8),
                          ),
                          Text(
                            addressList.isNotEmpty &&
                                    addrDeTime.isNotEmpty == true &&
                                    isLogin
                                ? (addrDeTime[0].description ?? '')
                                : S.current.address_delivery_address,
                            style: const TextStyle(
                              color: Colors.black,
                              fontSize: 16.4,
                              letterSpacing: TextConstant.letterSpacing,
                            ),
                          ),
                          const SizedBox(
                            width: 2,
                          ),
                          if (addressInfo == null)
                            Text(
                              S.current.Selec,
                              style: const TextStyle(
                                  color: Color.fromRGBO(84, 131, 191, 1),
                                  fontSize: FontConstant.f146,
                                  decoration: TextDecoration.underline,
                                  letterSpacing: TextConstant.letterSpacing),
                            )
                        ],
                      ),
                      Container(
                        padding: const EdgeInsets.only(top: 3,left: 21.8),
                        constraints: const BoxConstraints(maxWidth: 300),
                        child: Text(
                          addressInfo != null
                              ? addressInfo.address
                              : S.current.address_enter,
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          style: const TextStyle(
                              color: Color.fromRGBO(134, 141, 150, 1),
                              fontSize: 12.7,
                              letterSpacing: TextConstant.letterSpacing),
                        ),
                      )
                    ],
                  ),
                ),
              ),
              if (addressInfo != null)
                Image.asset(
                  Assets.assets_icons_grey_arrow_right_png,
                  height: 12,
                )
            ],
          ),
        ),
      ),
    );
  }
}
